package com.chufuying.tools;

import com.chufuying.tools.beans.GlobalClass;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

/**
 * supermarket表的数据源
 * @date:2015-09-10
 * @autor:chufuying
 * @version:1.0.0
 * */
public class DataSource {
	private  Connection conn=null;	
	//创建连接
	private void createConn()	
	{		
	    if(conn==null){
	 		try{	 			
	            Class.forName(GlobalClass.getDriver());
	            conn=DriverManager.getConnection(GlobalClass.getReadUrl(), GlobalClass.getReadname(), GlobalClass.getReadpassword());
	            System.out.println("DataBase link success! ");	                
	 			}catch(ClassNotFoundException e)
	 			{
	 				e.printStackTrace();
	 			}catch(SQLException e)
	 			{ 				
	 				System.out.println("DataBase can't link!");
	 				e.printStackTrace();
	 			}	
	 		}	
		}     
	//进行连接
	public  Connection getConn(){
    	if(conn==null){
    		createConn();	   
    	}
    	return conn;
    }
    //关闭连接
	public void closeConn(){
    	try{
    		conn.close();
    		conn=null;
    	}catch(SQLException e)
    	{
    		e.printStackTrace();
    	}
    }	
}
